#ifndef __ADAM_H
#define __ADAM_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "main.h"
#define SET485_MODBUS GPIO_SetBits(GPIOB, GPIO_Pin_0);delay_ms(10)
#define CLR485_MODBUS delay_us(500);GPIO_ResetBits(GPIOB, GPIO_Pin_0)

#define AdamId  0x01
#define iEM3000Id  0x01
#define ModbusReadRegCmd 0x03
#define ModbusRegBase 0x0000
#define DEVICE_TYPE 'C'
#define HEXTODEC(hex)  hexToDec((unsigned char *)(&hex),2)


typedef enum
{
    RE_UART_NULL=0,			//未接收
    RE_UART_NORMAL=0xFF,//正确需解析
    RE_UART_LENGH_ERR=0x01,//长度不对
    RE_UART_CRC16_ERR=0x02,//crc异常帧
    RE_UART_SLAVE_ERR=0x03,//从机不应答错误
	RE_UART_REV_START=0x04,//正确需解析
} RE_UART_TYPE;

typedef enum
{
    DATATYPE_UCHAR = 0,			//未接收
    DATATYPE_CHAR ,//正确需解析
    DATATYPE_USHORT,//长度不对
    DATATYPE_SHORT,//crc异常帧
    DATATYPE_ULONG,//从机不应答错误
    DATATYPE_LONG,
    DATATYPE_FLOAT,
    DATATYPE_DOUBLE,
} REG_DATA_TYPE;

typedef struct
{
    u8  MeterId;
    u8  ReadCmd;
    u16 RegAddress;
    u16 RegNumber;
	void (* pAnalyseFuntion)(u8 *pregdata);
} MODBUSMETER;

typedef struct 
{
 //公历日月年周
 u16 w_year;
 u16  w_month;
 u16  w_date; 
 u16 hour;
 u16 min;
 u16 sec; 
 u16 mmsec; 
}_calendar_obj; 

typedef struct 
{
 u8 MN[24];//
 u8 SN[24];
 u8 PassWord[9];   //访问密码
 u16 UpdataTimeSec;   //上传时间间隔
}_Config_obj; 

typedef struct            
{
//---------------------------------//
 float    fRTU_airtemp;    //空气温度
 float    fRTU_ahum;  //空气湿度
 float    fRTU_bpress;  //大气压强
 
 float    fRTU_noise;    //48
 float    fRTU_PM25;    //48
 float    fRTU_PM10;    //48 
 float    fRTU_Windspeed;    //48 
 float    fRTU_Winddir;    //48 
 _calendar_obj   Calendar;
 _Config_obj 	Config;
}ModbusRTUData;

void Uart5_SendString(u8*Str);
void AskForCurrent (void);
void Analyse_Modbusdata(void);
void AnalyseCmddata(void);
void AskForSensorTime(void);
void Modbus_UART_Init(u32 bound);
void Poll_Modbus_Task(void);

#endif
